package admin

import (
	"gf/app/model"
	"gf/app/service"
	"gf/library/response"
	"github.com/gogf/gf/net/ghttp"
)

var Article = articleApi{}

type articleApi struct {

}

func (a *articleApi) List(r *ghttp.Request) {
	var (
		data *model.ArticleApiListReq
	)
	if err := r.Parse(&data); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	getListRes, err := service.Article.List(r.Context(), data.ArticleGetListInput)
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	response.JsonExit(r, 0, "ok", getListRes)
}

func (a *articleApi) Add(r *ghttp.Request) {
	var (
		data *model.ArticleApiReq
	)
	if err := r.Parse(&data); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	id, err := service.Article.Add(r.Context(), data)
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	res := struct {
		Id int64
	}{Id:id}

	response.JsonExit(r, 0, "ok", res)
}

func (a *articleApi) Info(r *ghttp.Request) {
	var (
		data *model.ArticleApiInfoReq
	)
	if err := r.Parse(&data); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	result, err := service.Article.Info(r.Context(), data)
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	if result == nil{
		response.JsonExit(r, 404, "文章不存在")
	}

	response.JsonExit(r, 0, "ok",result)
}


func (a *articleApi) Edit(r *ghttp.Request) {
	var (
		data *model.ArticleApiSaveReq
	)
	if err := r.Parse(&data); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	result, err := service.Article.Edit(r.Context(), data)
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	response.JsonExit(r, 0, "ok",result)
}

func (a *articleApi) Del(r *ghttp.Request) {
	var (
		data *model.ArticleApiInfoReq
	)
	if err := r.Parse(&data); err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	_, err := service.Article.Del(r.Context(), data)
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}

	res := struct {
		Id int
	}{Id:data.Id}
	response.JsonExit(r, 0, "ok",res)
}
